package com.tingyuge.think.in.elasticsearch.service.impl;

import com.tingyuge.think.in.elasticsearch.service.PositionService;
import com.tingyuge.think.in.elasticsearch.util.DBHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/15
 */
@Service
@Slf4j
public class PositionServiceImpl implements PositionService {
    private static final Logger logger = LogManager.getLogger(PositionServiceImpl.class);
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private static final String POSITION_INDEX = "position";

    @Override
    public void importAll() throws Exception {
    mysqlDataToES("position");
    }

    @Override
    public List<Map<String, Object>> searchPos(String keyword, int pageNo, int pageSize) throws IOException {
        if(pageNo <1){
            pageNo = 1;
        }
        //搜索
        SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //分页设置
        searchSourceBuilder.from((pageNo - 1) * pageSize);
        searchSourceBuilder.size(pageSize);
        //精准匹配
        //TermQueryBuilder termQueryBuilder =QueryBuilders.termQuery("positionName",keyword);
        //searchSourceBuilder.query(termQueryBuilder);
        QueryBuilder builder = QueryBuilders.matchQuery("positionName",keyword);
        searchSourceBuilder.query(builder);
        searchSourceBuilder.timeout(new TimeValue(60,TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsMap());
        }
        return list;
    }

    private void mysqlDataToES(String tableName){
        BulkProcessor bulkProcessor = getBulkProcessor(restHighLevelClient);
        Connection conn =null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        conn = DBHelper.getConn();
        logger.info("start data: "+ tableName);
        String sql = "select * from "+tableName;
        try {
            preparedStatement = conn.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
            //根据需要设置 fetch size
            preparedStatement.setFetchSize(20);
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();

            HashMap<String,String> map =null;
            int count =0;
            //列值
            String column = null;

            String value = null;

            while (resultSet.next()){
                count ++;
                map = new HashMap<>(128);
                for (int i = 1; i < metaData.getColumnCount(); i++) {
                    column = metaData.getColumnName(i);
                    value = resultSet.getString(i);
                    map.put(column, value);
                }
                arrayList.add(map);
                //每10000万条 1次 不足的数据最后一次处理
                if(count % 10000  ==0){
                    logger.info(" mysql data  number : = "+ count);
                    //将数据添加到 bulkProcessor
                    for (HashMap<String, String> stringStringHashMap : arrayList) {
                        bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(stringStringHashMap));
                    }
                    //没提交一次 清空一次list 和map
                    arrayList.clear();
                    map.clear();
                }



            }
            //最后处理未提交的数据
            for (HashMap<String, String> stringStringHashMap : arrayList) {
                bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(stringStringHashMap));
            }
            bulkProcessor.flush();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                resultSet.close();
                preparedStatement.close();
                conn.close();
                boolean flag = bulkProcessor.awaitClose(150, TimeUnit.SECONDS);
                logger.info(flag);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private BulkProcessor getBulkProcessor(RestHighLevelClient client) {
        BulkProcessor bulkProcessor = null;
        try {
            BulkProcessor.Listener listener = new BulkProcessor.Listener() {
                @Override
                public void beforeBulk(long executionId, BulkRequest request) {
                    logger.info("Try to insert data number : " + request.numberOfActions());
                }

                @Override
                public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
                    logger.info("************** Success insert data number : " + request.numberOfActions() + " , id: " + executionId);
                }

                @Override
                public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                    logger.error("Bulk is unsuccess : " + failure + ", executionId: " + executionId);
                }
            };
            BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
                    (request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
            BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer, listener);
            builder.setBulkActions(5000);
            //或者100M一次
            builder.setBulkSize(new ByteSizeValue(100L, ByteSizeUnit.MB));
            //并发请求
            builder.setConcurrentRequests(10);
            //刷新间隔
            builder.setFlushInterval(TimeValue.timeValueSeconds(100L));
            builder.setBackoffPolicy(BackoffPolicy.constantBackoff(TimeValue.timeValueSeconds(1L), 3));
// 注意点：让参数设置生效BulkProcessor 官网介绍https:
//www.elastic.co/guide/en/elasticsearch/client/java-api/7.3/java-docs-bulk-processor.html
            bulkProcessor = builder.build();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                bulkProcessor.awaitClose(100L, TimeUnit.SECONDS);
            } catch (Exception e1) {
                logger.error(e1.getMessage());
            }
        }
        return bulkProcessor;
    }
}
